package br.com.smart.logistica.services;

import java.util.ArrayList;

import br.com.logistica.base.BaseException;
import br.com.logistica.database.ClienteDAO;
import br.com.logistica.database.NotaFiscalDAO;
import br.com.logistica.database.ProdutoDAO;
import br.com.logistica.util.dto.ServiceDTO;
import br.com.logistica.vo.Cliente;
import br.com.logistica.vo.NotaFiscal;
import br.com.logistica.vo.Produto;
import br.com.util.BeanUtil;

public class NotaFiscalSCN {

	private static ServiceDTO result = null;

	public static ServiceDTO pesquisar(ServiceDTO requestDTO) throws Exception {

		NotaFiscal notaFiscalVO = (NotaFiscal) requestDTO.get("notaFiscal");

		br.com.logistica.persistence.entidades.NotaFiscal notaFiscalPersistence = BeanUtil
				.castNotaFiscalVoNotaFiscalPersistence(notaFiscalVO);

		requestDTO.set("notaFiscal", notaFiscalPersistence);

		NotaFiscalDAO notaFiscalDAO = new NotaFiscalDAO();

		ArrayList<NotaFiscal> notaFiscals = new ArrayList<NotaFiscal>();
		ArrayList<br.com.logistica.persistence.entidades.NotaFiscal> arrayListPersistence = (ArrayList<br.com.logistica.persistence.entidades.NotaFiscal>) notaFiscalDAO
				.retrieve(requestDTO).get("notaFiscal");

		for (br.com.logistica.persistence.entidades.NotaFiscal notaFiscal : arrayListPersistence) {

			notaFiscals.add(BeanUtil
					.castNotaFiscalPersistenceNotaFiscalVo(notaFiscal));
		}

		requestDTO.set("notaFiscal", notaFiscals);
		return requestDTO;

	}

	public static ServiceDTO insereCliente(ServiceDTO requestDTO)
			throws Exception {
		NotaFiscal notaFiscalVO = (NotaFiscal) requestDTO.get("notaFiscal");

		br.com.logistica.persistence.entidades.NotaFiscal notaFiscalPersistence = BeanUtil
				.castNotaFiscalVoNotaFiscalPersistence(notaFiscalVO);

		NotaFiscalDAO notaFiscalDAO = new NotaFiscalDAO();

		requestDTO = notaFiscalDAO.retrieve(requestDTO);

		requestDTO.set("notaFiscal", requestDTO);

		notaFiscalPersistence = (br.com.logistica.persistence.entidades.NotaFiscal) requestDTO
				.get("notaFiscal");

		if (notaFiscalPersistence == null) {

			notaFiscalDAO.create(requestDTO);

		} else {

			notaFiscalDAO.update(requestDTO);

		}

		return requestDTO;

	}

	public static ServiceDTO deleteCliente(ServiceDTO requestDTO)
			throws Exception {

		NotaFiscal notaFiscalVO = (NotaFiscal) requestDTO.get("notaFiscal");

		br.com.logistica.persistence.entidades.NotaFiscal notaFiscalPersistence = BeanUtil
				.castNotaFiscalVoNotaFiscalPersistence(notaFiscalVO);

		NotaFiscalDAO notaFiscalDAO = new NotaFiscalDAO();

		requestDTO.set("notaFiscal", notaFiscalPersistence);

		notaFiscalDAO.delete(requestDTO);

		return requestDTO;

	}

	public static ServiceDTO pesquisar() throws Exception {
		ServiceDTO responseDTO = new ServiceDTO();
		NotaFiscalDAO dao = new NotaFiscalDAO();
		responseDTO = dao.retrieve();
		ArrayList<br.com.logistica.persistence.entidades.NotaFiscal> produtos = (ArrayList<br.com.logistica.persistence.entidades.NotaFiscal>) responseDTO
				.get("notaFiscal");

		ArrayList<NotaFiscal> arrayList = new ArrayList<NotaFiscal>();
		for (br.com.logistica.persistence.entidades.NotaFiscal notaFiscal : produtos) {
			arrayList.add(BeanUtil
					.castNotaFiscalPersistenceNotaFiscalVo(notaFiscal));
		}
		responseDTO.set("notaFiscal", arrayList);
		return responseDTO;
	}

}
